// 测试环境

// var baseUrl = 'https://api.manfangbao.com'; //正式环境

var baseUrl = uni.getStorageSync('url'); //测试环境






const request = (params) => {

	if (!params.data) {
		params.data = {}
	}
	let defParams = {
		// token: uni.getStorageSync('token')||''
		
	}
	let unParams = {
		...defParams,
		...params.data,
	}
	
	if(uni.getStorageSync('token')){
		var Authorization='tf: '+uni.getStorageSync('token')
	}else{
		
	}
	let header = {
		...defParams,
		'Content-Type': params.headerType ? params.headerType : 'application/json',
		'Authorization':Authorization
	}
	
	var url = baseUrl + params.url
	
	if(params.data.notoken&&params.data.notoken==1){
		header.Authorization=''
	}



	var httpDefaultOpts = {
		url: url,
		data: unParams,
		method: params.method.toUpperCase(),
		timeout: 50000,
		header: header,
		dataType: 'json',
		responseType: params.responseType ?  params.responseType : ''
	}
	
	// console.log(httpDefaultOpts)
	return new Promise(function(resolve, reject) {

		uni.showLoading({
			mask: true,
			title: '请稍等'
		})
		uni.request(httpDefaultOpts).then(

			(res) => {
				console.log(res)
				if (res.statusCode == 200||res.statusCode == 201) {
					
					resolve(res.data)
				} else {
					console.log(res,url)
				}
				uni.hideLoading()
			}
		).catch(
			(response) => {
				uni.hideLoading()
				console.log(response, 'response======', url);
				// uni.hideLoading();
				uni.$u.toast('网络出错了');
				// uni.navigateTo({
				// 	url:'/pages/index/nonetwork'
				// })
				reject(response)
			}
		)
	})
};




export {
	request,
	baseUrl
}